package com.xingkeduo.business.order.remote;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.order.domain.OrderDomainService;
import com.xingkeduo.business.order.remote.feign.MarketingFeignService;
import com.xingkeduo.business.order.service.OrderService;
import com.xingkeduo.dto.ActivityParam;
import com.xingkeduo.dto.ActivityUseParam;
import com.xingkeduo.dto.MarketActivityDto;
import com.xingkeduo.dto.param.ActivityCombinationParam;
import com.xingkeduo.pojo.table.Order;
import com.xingkeduo.utils.DateTimeUtils;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Slf4j
public class MarketingRemoteService {

    @Resource
    private MarketingFeignService marketingFeignService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDomainService orderDomainService;


    public MarketActivityDto getMarketActivityInfo(Order order){

        ActivityCombinationParam activityCombinationParam = new ActivityCombinationParam();
        //门店id
        activityCombinationParam.setStoreId(Long.valueOf(order.getStoreId()));
        //发型师id
        activityCombinationParam.setHairdresserId(Long.valueOf(order.getAppointmentAdminId()));
        //该用户关联的用户ids
        activityCombinationParam.setUserId(null);
        //当前登陆用户id
        activityCombinationParam.setUserId(Long.valueOf(order.getUserId()));
        //用户类型 用户类型 0 全部用户，1 星客多新用户，2 门店新用户
        activityCombinationParam.setUserType(orderService.queryUserType(order.getUserId(),order.getStoreId()));
        //服务id
        activityCombinationParam.setHairServiceId(Long.valueOf(order.getServiceTypeId()));
        //服务原价格
        activityCombinationParam.setHairServiceOriginPrice(order.getAllPrice());
        //预约时间
        activityCombinationParam.setAppointmentDate(DateTimeUtils.yyyy_MM_dd_HH_mm_ss(order.getAppointmentStartTime()));

        activityCombinationParam.setNewUserType(String.valueOf(orderService.queryUserType(order.getUserId(),order.getStoreId())));
        log.info("getMarketActivityInfo activityCombinationParam:{}", JSON.toJSONString(activityCombinationParam));
        ResponseUtils<MarketActivityDto> responseUtils= marketingFeignService.getRecommendedPackage(activityCombinationParam);
        MarketActivityDto marketActivityDto = responseUtils.getData();

        return marketActivityDto;

    }


    /**
     * 核对订单 修改活动信息
     * @param activityId
     * @param marketingReceiveHistoryId
     * @param preferentialType
     * @return
     */
    public MarketActivityDto getRecommendedPackage(String activityId,String marketingReceiveHistoryId,Integer preferentialType, Order order,String type){
        ActivityUseParam activityUseParam = new ActivityUseParam();

        if(StringUtils.isNotEmpty(activityId)){
            activityUseParam.setActivityId(Long.valueOf(activityId));
        }

        if(StringUtils.isNotEmpty(marketingReceiveHistoryId)){
            activityUseParam.setMarketingReceiveHistoryId(Long.valueOf(marketingReceiveHistoryId));
        }
        activityUseParam.setPreferentialType(preferentialType);
        activityUseParam.setHairServiceId(Long.valueOf(order.getServiceTypeId()));
        activityUseParam.setStoreId(Long.valueOf(order.getStoreId()));
        activityUseParam.setUserId(Long.valueOf(order.getUserId()));

        activityUseParam.setHairServiceOriginPrice(order.getAllPrice());
        activityUseParam.setAppointmentDate(DateTimeUtils.yyyy_MM_dd_HH_mm_ss(order.getAppointmentStartTime()));
        if("0".equals(type)){
            if(StringUtils.isNotEmpty(order.getMarketingType())){
                activityUseParam.setMarketingType(Integer.valueOf(order.getMarketingType()));
            }
        }else {
            activityUseParam.setMarketingType(Integer.valueOf(type));

        }


        log.info("getRecommendedPackage activityUseParam:{}", JSON.toJSONString(activityUseParam));
        ResponseUtils<MarketActivityDto> responseUtils= marketingFeignService.checkUserActivity(activityUseParam);
        MarketActivityDto marketActivityDto = responseUtils.getData();
        orderDomainService.freeCardUse(marketActivityDto,order);
        return marketActivityDto;
    }


    /**
     *
     * @param order
     * @return
     */
    public boolean getStoreBypassFlowActivityDetail(Order order){

        ActivityParam activityParam = new ActivityParam();
        activityParam.setStoreId(Long.valueOf(order.getStoreId()));
        activityParam.setHairdresserId(Long.valueOf(order.getAdminId()));
        activityParam.setUserId(Long.valueOf(order.getUserId()));
        activityParam.setUserType(orderService.queryUserType(order.getUserId(),order.getStoreId()));
        activityParam.setHairServiceId(Long.valueOf(order.getServiceTypeId()));
        activityParam.setHairServiceOriginPrice(order.getAllPrice());
        activityParam.setAppointmentDate(DateTimeUtils.yyyy_MM_dd_HH_mm_ss(order.getAppointmentStartTime()));
        log.info("支付后，修改时间获取活动如参activityParam:{}", JSON.toJSONString(activityParam));
        ResponseUtils<MarketActivityDto> responseUtils= marketingFeignService.getStoreBypassFlowActivityDetail(activityParam);
        log.info("支付后，修改时间获取活动信息:{}", JSON.toJSONString(responseUtils));
        log.info("支付后，订单活动id:{}", order.getActivityId());


        if(responseUtils!=null){
            MarketActivityDto marketActivityDto = responseUtils.getData();
            if(StringUtils.isEmpty(order.getActivityId()) && marketActivityDto==null){
                log.info("修改预约时间，活动id为空,分流活动为null");
                return true;
            }

            if(StringUtils.isEmpty(order.getActivityId()) && marketActivityDto.getActivityId() == null){
                log.info("修改预约时间，活动id为空,分流活动为id为空");
                return true;
            }
            if(marketActivityDto !=null && StringUtils.isNotEmpty(order.getActivityId()) && marketActivityDto.getActivityId() != null && order.getActivityId().equals(String.valueOf(marketActivityDto.getActivityId()))){
                BigDecimal activityReduceAmount =BigDecimal.ZERO;
                Integer activityType = marketActivityDto.getActivityType();
                if (activityType != null) {
                    BigDecimal activityPrice = marketActivityDto.getActivityDenominationAmount();
                    switch (activityType) {//优惠方式 0 立减，1 折扣，2 特价
                        case 0:
                            //如果立减价格小于原价，优惠价格为立减价格，否则优惠价为原价
                            activityReduceAmount =order.getAllPrice().compareTo(activityPrice) == 1 ? activityPrice : order.getAllPrice();
                            break;
                        case 1:
                            //总价*折扣
                            activityReduceAmount=order.getAllPrice().multiply((BigDecimal.ONE.subtract(activityPrice)));
                            break;
                        case 2:
                            // 总价格-特价
                            activityReduceAmount=order.getAllPrice().compareTo(activityPrice) == 1 ? order.getAllPrice().subtract(activityPrice) : BigDecimal.ZERO;
                            break;
                    }
                }

                if(order.getActivityPrice().compareTo(activityReduceAmount)==0){
                    log.info("修改预约时间，活动id一致可以修改");
                    return true;
                }
            }
        }
        log.info("修改预约时间，不可以修改");
        return false;
    }



    /**
     * 退卡 退券
     * @param order
     * @return
     */
    public boolean refundOrder(Order order){
        if(bfDate(order)){
            ActivityUseParam activityUseParam = assemblyData(order.getOrderNo());
            marketingFeignService.refundOrder(activityUseParam);
            return true;
        }
        return false;
    }


    /**
     * 下单成功修改用户券，卡状态
     * @param order
     * @return
     */
    public boolean placeOrder(Order order){
        if(bfDate(order)){
            ActivityUseParam activityUseParam = assemblyData(order.getOrderNo());
            marketingFeignService.placeOrder(activityUseParam);
            return true;
        }
        return false;
    }


    public ActivityUseParam assemblyData(String orderNo){
        Order order = orderService.queryOrderByOrderNO(orderNo);
        ActivityUseParam activityUseParam = new ActivityUseParam();
        if(StringUtils.isNotEmpty(order.getMarketingId())){
            activityUseParam.setMarketingReceiveHistoryId(Long.valueOf(order.getMarketingId()));
            activityUseParam.setMarketingType(Integer.valueOf(order.getMarketingType()));
        }

       if(StringUtils.isNotEmpty(order.getActivityId())){
            activityUseParam.setActivityId(Long.valueOf(order.getActivityId()));
        }
        activityUseParam.setUserId(Long.valueOf(order.getUserId()));
        activityUseParam.setOrderId(order.getId());
        activityUseParam.setStoreId(Long.valueOf(order.getStoreId()));
        activityUseParam.setAppointmentDate(DateTimeUtils.yyyy_MM_dd_HH_mm_ss(order.getAppointmentStartTime()));
        activityUseParam.setHairStyleId(Long.valueOf(order.getAppointmentAdminId()));
        activityUseParam.setReduceActivityAmount(order.getActivityPrice());
        activityUseParam.setReduceMarketingAmount(order.getMarketingPrice());
        activityUseParam.setStoreName(order.getStoreName());
        return activityUseParam;
    }

    public boolean bfDate(Order order){
        if(order.getMarketingId() !=null || order.getActivityId() !=null){
            return true;
        }
       return false;
    }

}
